package de.home.spring.backend.dao;

import java.util.List;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import de.home.spring.backend.IDAO;
import de.home.spring.backend.dao.rowmappers.PersonRowMapper;
import de.home.spring.beans.Person;

public class PersonDAO extends NamedParameterJdbcDaoSupport implements IDAO<Person> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 642772240698739600L;

	public List<Person> selectAll() {
		String query = "Select * from Person";
		List<Person> persons = getJdbcTemplate().query(query, new PersonRowMapper());
		return persons;
	}

	public void write(Person person) {
		String query = "insert into Person values (:age, :name)";
		MapSqlParameterSource namedParameters = new MapSqlParameterSource();
	    namedParameters.addValue("age", person.getAge());
	    namedParameters.addValue("name", person.getName());
	    getNamedParameterJdbcTemplate().update(query, namedParameters);
	    
		query = "insert into Person values (:age, :name)";
		namedParameters = new MapSqlParameterSource();
	    namedParameters.addValue("age", person.getAge());
	    namedParameters.addValue("name", person.getName());
	    getNamedParameterJdbcTemplate().update(query, namedParameters);
	}
	
}
